﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class DanhSachXeForm : Office2007Form
    {
        private readonly ThemBaoGiaForm form;
        private readonly QuanLyPhieuBaoGia frmquanly;
        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
       
        public DanhSachXeForm(ThemBaoGiaForm form1)
        {
            InitializeComponent();
            this.form = form1;
        }
        public DanhSachXeForm(QuanLyPhieuBaoGia form1)
        {
            InitializeComponent();
            this.frmquanly = form1;
        }
        void DanhSachXe()
        {
            var dsxe = from xe in BO.DanhSachXeBO.DanhSachXe()
                       select new { xe.BienSoXe, xe.DONGXE.TenDongXe, xe.HANGSX.TenHangSanXuat, xe.DOIXE.TenDoiXe, xe.SoSuon};
            //gvXe.DataSource = dsxe;
            
        }


        private void btnThem_Click(object sender, EventArgs e)
        {
            
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
           
        }

        private void DanhSachXeForm_Load(object sender, EventArgs e)
        {
            cbHangSX.DataSource = BO.DanhSachXeBO.DanhSachHangSanXuat();
            cbHangSX.DisplayMember = "TenHangSanXuat";
            cbHangSX.ValueMember = "MaHangSanXuat";

           

            
            cbDongXe.DataSource = BO.DanhSachXeBO.DanhSachDongXeTheoHangSanXuat(cbHangSX.SelectedValue.ToString());
            cbDongXe.DisplayMember = "TenDongXe";
            cbDongXe.ValueMember = "MaDongXe";

            cbDoiXe.DataSource = BO.DanhSachXeBO.DanhSachDoiXeTheoDongXe(cbDongXe.SelectedValue.ToString());
            cbDoiXe.DisplayMember = "TenDoiXe";
            cbDoiXe.ValueMember = "MaDoiXe";
            DanhSachXe();
        }

        private void gvXe_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void textBoxX2_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnThem_Click_1(object sender, EventArgs e)
        {
            bool kt = false;
            if(txtBienSoXe.Text.Trim()=="")
            {
                MessageBox.Show("Không được để trống biển số xe");
                txtBienSoXe.Focus();
                kt = true;
                return;
            }
            if (txtSoSuon.Text.Trim() == "")
            {
                MessageBox.Show("Không được để trống số sườn");
                txtSoSuon.Focus();
                kt = true;
                return;
            }
            if(kt==false)
            {
                BO.DanhSachXeBO.ThemXe(txtBienSoXe.Text.Trim(), txtSoSuon.Text.Trim(), cbDongXe.SelectedValue.ToString(), cbHangSX.SelectedValue.ToString(), cbDoiXe.SelectedValue.ToString());
                DanhSachXe();
                if (this.form != null)
                    this.form.LoadBienSo();
                if (this.frmquanly != null)
                    this.frmquanly.LoadXe();
                MessageBox.Show("Bạn đã thêm thành công");


            }
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gvXe_RowEnter_1(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    int index = e.RowIndex;

            //    //ThemPhieuBaoGiaForm.maxe = gvXe.Rows[index].Cells["BienSoXe"].Value.ToString().Trim();
            //    QuanLyBaoGia.maxe = gvXe.Rows[index].Cells["BienSoXe"].Value.ToString().Trim();
            //}
            //catch
            //{
            //    MessageBox.Show("Không có xe nào");
            //}
        }

        private void cbHangSX_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDongXe.DataSource = BO.DanhSachXeBO.DanhSachDongXeTheoHangSanXuat(cbHangSX.SelectedValue.ToString());
            cbDongXe.DisplayMember = "TenDongXe";
            cbDongXe.ValueMember = "MaDongXe";
        }

        private void cbDoiXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void cbDongXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDoiXe.DataSource = BO.DanhSachXeBO.DanhSachDoiXeTheoDongXe(cbDongXe.SelectedValue.ToString());
            cbDoiXe.DisplayMember = "TenDoiXe";
            cbDoiXe.ValueMember = "MaDoiXe";
        }
    }
}
